package com.wetech.demo.web3j.service;
import org.springframework.stereotype.Service;
import org.web3j.protocol.Web3j;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import java.util.concurrent.CompletableFuture;
import com.wetech.demo.web3j.contracts.lxltoken.LXLToken;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.math.BigInteger;
@Slf4j
@Service
@RequiredArgsConstructor
public class LXLTokenService {
    private final Web3j web3j;
    private final Credentials credentials;
    private final ContractGasProvider gasProvider;
    private LXLToken contract;
    @Getter
    private String contractAddress;
    /**
     * Deploy the SimpleStorage contract to the blockchain
     * @return the address of the deployed contract
     */
    public CompletableFuture<String> deployContract() {
        log.info("Deploying SimpleStorage contract...");
        return LXLToken.deploy(web3j, credentials, gasProvider)
                .sendAsync()
                .thenApply(contract -> {
                    this.contract = contract;
                    this.contractAddress = contract.getContractAddress();
                    log.info("LXLToken contract deployed to: {}", contractAddress);
                    return contractAddress;
                });
    }
    /**
     * Load an existing contract from the blockchain
     * @param contractAddress the address of the contract to load
     */
    public void loadContract(String contractAddress) {
        log.info("Loading SimpleStorage contract from address: {}", contractAddress);
        this.contract = LXLToken.load(contractAddress, web3j, credentials, gasProvider);
        this.contractAddress = contractAddress;
    }
    // 铸币
    public CompletableFuture<String> mint(BigInteger amount) {
        if (contract == null) throw new IllegalStateException("Contract not loaded");
        log.info("Mint {} tokens", amount);
        return contract.mint(amount)
                .sendAsync()
                .thenApply(TransactionReceipt::getTransactionHash);
    }
    // 转账
    public CompletableFuture<String> transfer(String to, BigInteger amount) {
        if (contract == null) {
            throw new IllegalStateException("Contract not deployed or loaded");
        }
        log.info("transfer from contract to address: {}", to);
        return contract.transfer(to, amount)
                .sendAsync()
                .thenApply(TransactionReceipt::getTransactionHash);
    }
    // 查询余额
    public CompletableFuture<BigInteger> getBalance(String address) {
        if (contract == null) throw new IllegalStateException("Contract not loaded");
        return contract.balanceOf(address).sendAsync();
    }
    // 授权
    public CompletableFuture<String> approve(String spender, BigInteger value) {
        if (contract == null) throw new IllegalStateException("Contract not loaded");
        log.info("Approve {} tokens for spender {}", value, spender);
        return contract.approve(spender, value)
                .sendAsync()
                .thenApply(TransactionReceipt::getTransactionHash);
    }
    //转账
    public CompletableFuture<String> transferFrom(String from, String to, BigInteger value) {
        if (contract == null) throw new IllegalStateException("Contract not loaded");
        log.info("TransferFrom {} -> {} : {}", from, to, value);
        return contract.transferFrom(from, to, value)
                .sendAsync()
                .thenApply(TransactionReceipt::getTransactionHash);
    }
}
